//封装网络请求
module.exports = function(path,data,method){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: 'http://127.0.0.1:8080/WeixinWeb/' + path,//
      method: method,
      data: data,
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: res=>{
        if(res.statusCode != 200){
          fail('服务器异常',reject);
          return;
        }
        if(!res.data.success){
          fail(res.data.msg,reject);
          return;
        }
        //访问成功，返回响应的数据，在promise的then中回调
        resolve(res.data)
      },
      fail:()=>{
        fail('加载数据失败',reject);
      }
    }),

    function fail(title,callback){
      wx.hideLoading();
      wx.showModal({
        title:title,
        confirmText: '重试',
        success: res=>{
          if(res.confirm){
            callback();
          }
        }
      })
    }
  })
}